package com.epoint.webapp.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.epoint.webapp.dao.BlogDAO;
import com.epoint.webapp.dao.BlogMessageDAO;
import com.epoint.webapp.entity.Blog;
import com.epoint.webapp.entity.BlogMessage;
import com.epoint.webapp.entity.Member;
import com.epoint.webapp.util.PageCount;

@Controller
public class BlogController {
	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-module.xml");
	
	@RequestMapping(value = "/showBlog", method = RequestMethod.GET)
	public ModelAndView getBlog(HttpServletRequest request, HttpSession session, int id) throws IOException {
		ModelAndView model = new ModelAndView();
		BlogDAO blogDAO = (BlogDAO) context.getBean("blogDAO");
		BlogMessageDAO blogMessageDAO = (BlogMessageDAO) context.getBean("blogMessageDAO");
		Blog blog = new Blog();
		blog.setId(id);
		blog = blogDAO.get(blog);
		
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		int pageSize = 10;
		int count = blogMessageDAO.countTotal(id);
		String pageUrl = "showBlog";
		String pageEndUrl = "&id="+id+"#messageArea";
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl);
		pageCount.setEndUrl(pageEndUrl);
		pageCount.calculate();
		
		List<BlogMessage> blogMessageList = new ArrayList<BlogMessage>();
		blogMessageList = blogMessageDAO.getList(id, pageCount.getStart(), pageSize);
		Member memberLogin = (Member)session.getAttribute("loginMember");
		if(memberLogin != null){
			for(int i=0; i<blogMessageList.size(); i++){
				blogMessageList.get(i).setWriter(memberLogin.getAccount().equals(blogMessageList.get(i).getAccount())? true : false);
			}
			model.setViewName("blogDetail1");
		}
		else{
			model.setViewName("blogDetail2");
		}
		model.addObject("blogMessageList", blogMessageList);
		model.addObject("blog", blog);
		model.addObject("page", pageCount);
		return model;
	}
	
	@RequestMapping(value = "/blog", method = RequestMethod.GET)
	public ModelAndView getBlogList(HttpServletRequest request, HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		BlogDAO blogDAO = (BlogDAO) context.getBean("blogDAO");
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		int pageSize = 10;
		int count = blogDAO.countTotal();
		String pageUrl = "blog";
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, "");
		pageCount.calculate();
		
		List<Blog> blogList = new ArrayList<Blog>();
		blogList = blogDAO.getList(pageCount.getStart(), pageSize);
		model.addObject("blogList", blogList);
		model.addObject("page", pageCount);
		model.setViewName("blog");
		return model;
	}
	
	@RequestMapping(value = "/management/manageBlog", method = RequestMethod.GET)
	public ModelAndView manageBlog(HttpServletRequest request, HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();
		BlogDAO blogDAO = (BlogDAO) context.getBean("blogDAO");
		
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		int pageSize = 10;
		int count = blogDAO.countTotal();
		String pageUrl = "manageBlog";
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, "");
		pageCount.calculate();
		
		List<Blog> blogList = new ArrayList<Blog>();
		blogList = blogDAO.getList(pageCount.getStart(), pageSize);
		model.addObject("blogList", blogList);
		model.addObject("page", pageCount);
		model.setViewName("management/manageBlog");
		return model;
	}
	
	@RequestMapping(value = "/management/insertBlogCont", method = RequestMethod.GET)
	public ModelAndView insertBlogCont(HttpServletRequest request, HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();	
		model.setViewName("management/insertBlog");
		return model;
	}
	
	@RequestMapping(value = "/management/insertBlog", method = RequestMethod.POST)
	public ModelAndView insertBlog(HttpServletRequest request, HttpSession session, Blog blog) throws IOException {
		ModelAndView model = new ModelAndView();		
		BlogDAO blogDAO = (BlogDAO) context.getBean("blogDAO");
		blogDAO.insert(blog);
		model.setViewName("redirect:/management/manageBlog");
		return model;
	}
	
	
	@RequestMapping(value = "/management/updateBlogCont", method = RequestMethod.GET)
	public ModelAndView updateBlogCont(HttpServletRequest request, HttpSession session, int id) throws IOException {
		ModelAndView model = new ModelAndView();
		BlogDAO blogDAO = (BlogDAO) context.getBean("blogDAO");
		Blog blog = new Blog();
		blog.setId(id);
		blog = blogDAO.get(blog);
		model.addObject("blog", blog);
		model.setViewName("management/updateBlog");
		return model;
	}
	
	@RequestMapping(value = "/management/updateBlog", method = RequestMethod.POST)
	public ModelAndView updateBlog(HttpServletRequest request, HttpSession session, Blog blog) throws IOException {
		ModelAndView model = new ModelAndView();
		BlogDAO blogDAO = (BlogDAO) context.getBean("blogDAO");
		blogDAO.update(blog);
		model.setViewName("redirect:/management/manageBlog");
		return model;
	}

	@RequestMapping(value = "/management/deleteBlog", method = RequestMethod.POST)
	public ModelAndView deleteBlog(HttpServletRequest request, HttpSession session, int id) throws IOException {
		ModelAndView model = new ModelAndView();
		BlogDAO blogDAO = (BlogDAO) context.getBean("blogDAO");
		Blog blog = new Blog();
		blog.setId(id);
		blogDAO.delete(blog);
		model.setViewName("redirect:/management/manageBlog");		
		return model;
	}
}
